package com.oolatina;

import com.oolatina.SCButton.SCButtonListener;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.HorizontalScrollView;
import android.widget.Button;

public class SCScrollSelector implements OnClickListener
{
	int _width;
	int _height;
	int _x;
	int _y;
	int _xpos;
	Context _context;
	int ScreenWidth;
	int ScreenHeight;
	HorizontalScrollView scroll;
	FrameLayout _frame;
	int _number;
	SCScrollSelectorListener listener;
	
	// Delegate Method
	public interface SCScrollSelectorListener 
	{
	    public void selectElement(int elementSelected);
	}

	public void init(LinearLayout layout, Context thisContext,SCScrollSelectorListener _listener)
	{
		this.listener = _listener;
		_width = 0;
		_height = 0;
		_x = 0;
		_y = 0;
		_number = 0;
		_xpos = 0;
		_context = thisContext;
		
		WindowManager wm = (WindowManager) thisContext.getSystemService(Context.WINDOW_SERVICE);
	    Display display = wm.getDefaultDisplay();
	    ScreenWidth = display.getWidth();
	    ScreenHeight = display.getHeight();
		 
	    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ScreenWidth, 60);
	    layoutParams.setMargins(0,0,0,0);
	    
	    scroll = new HorizontalScrollView(thisContext);
	    scroll.setLayoutParams(layoutParams);
	    layout.addView(scroll);
	    
	    LinearLayout.LayoutParams framelayoutParams = new LinearLayout.LayoutParams(
				ScreenWidth*2,60);
	    
	    _frame = new FrameLayout(thisContext);
	    _frame.setLayoutParams(framelayoutParams);
	    scroll.addView(_frame);
	}
	
	
	public int GetY() {
		return _y;
	}
	
	public void SetPosition(int x,int y)
	{
		_x = x;
		_y = y;
		
		FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
				_width,_height);
        layoutParams.setMargins(_x,_y,0,0);
        scroll.setLayoutParams(layoutParams);
	}
	
	public void SetPourcentPosition(int x, int y)
	{
		float ratioWidth = (float)ScreenWidth/100;
		float ratioHeight = (float)ScreenHeight/100;
		_x = (int)(ratioWidth*x);
		_y = (int)(ratioHeight*y);
		
		FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
				_width,_height);
//        layoutParams.setMargins(_x,_y,0,0);
        scroll.setLayoutParams(layoutParams);
	}
	
	public void SetSize(int width,int height)
	{
		_width = width;
		_height = height;
		
		FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
				_width,_height);
        layoutParams.setMargins(_x,_y,0,0);
        scroll.setLayoutParams(layoutParams);
	}
	
	public void SetPourcentSize(int width, int height)
	{
		float radioWidth = (float)ScreenWidth/100;
    	float radioHeight = (float)ScreenHeight/100;
    	_width = (int)(radioWidth*width);
    	_height = (int)(radioHeight*height);
		
    	FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
				_width,_height);
        layoutParams.setMargins(_x,_y,0,0);
        scroll.setLayoutParams(layoutParams);
	}
	
	public int SetPourcentWidth(int width)
	{
		float radioWidth = (float)ScreenWidth/100;
		_width = (int)(radioWidth*width);
		
		FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
				_width,_height);
        layoutParams.setMargins(_x,_y,0,0);
        scroll.setLayoutParams(layoutParams);
        return _width;
	}
	
	public int SetPourcentHeight(int height)
	{
		float radioHeight = (float)ScreenHeight/100;
		_height = (int)(radioHeight*height);
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				_width,_height);
//        layoutParams.setMargins(_x,_y,0,0);
        scroll.setLayoutParams(layoutParams);
        return _height;
	}
	
	public void SetBackgroundColor(int alpha,int red,int green,int blue)
    {
		scroll.setBackgroundColor(Color.argb(alpha,red,green,blue));
    }
	
	public void addElement(int width, int height, int idimage)
	{
		float radioWidth = (float)ScreenWidth/100;
		int wWidth = (int)(radioWidth*width);
		
		FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
				wWidth,height);
        layoutParams.setMargins(_xpos,0,0,0);
		
		Button nButton = new Button(_context);
		nButton.setBackgroundResource(idimage);
		nButton.setOnClickListener(this);
		nButton.setTag(_number);
		nButton.setLayoutParams(layoutParams);
		_frame.addView(nButton);
		
		_number = _number+1;
		_xpos = _xpos + wWidth + 10;
		
		// On agrandie la zone de frame
		FrameLayout.LayoutParams framelayoutParams = new FrameLayout.LayoutParams(
				_xpos,_height);
		_frame.setLayoutParams(framelayoutParams);
	}
	
	@Override
    public void onClick(View v) 
	{
		int tTag = (Integer)v.getTag();
		this.listener.selectElement(tTag);
    }
}
